<?php
global $_W, $_GPC;
$weid = $this->_weid;
$action = 'setting';
$title = '系统设置';
$GLOBALS['frames'] = $this->getMainMenu();

load()->func('tpl');

$stores = pdo_fetchall("SELECT * FROM " . tablename($this->table_stores) . " WHERE weid = :weid ORDER BY `id` DESC", array(':weid' => $_W['uniacid']));
if (empty($stores)) {
    $url = $this->createWebUrl('stores', array('op' => 'display'));
//    message('请先添加门店', $url);
}

$setting = pdo_fetch("SELECT * FROM " . tablename($this->table_setting) . " WHERE weid = :weid", array(':weid' => $_W['uniacid']));
if (checksubmit('submit')) {
    $data = array(
        'weid' => $_W['uniacid'],
        'title' => trim($_GPC['title']),
        'thumb' => trim($_GPC['thumb']),
        'storeid' => intval($_GPC['storeid']),
        'entrance_type' => intval($_GPC['entrance_type']),
        'entrance_storeid' => intval($_GPC['entrance_storeid']),
        'order_enable' => intval($_GPC['order_enable']),
        'mode' => intval($_GPC['mode']),
        'is_notice' => intval($_GPC['is_notice']),
        'dining_mode' => intval($_GPC['dining_mode']),
        'istplnotice' => intval($_GPC['istplnotice']),
        'tplneworder' => trim($_GPC['tplneworder']),
        'tplnewqueue' => trim($_GPC['tplnewqueue']),
        'tploperator' => trim($_GPC['tploperator']),
        'searchword' => trim($_GPC['searchword']),
        'tpluser' => trim($_GPC['tpluser']),
        'tpltype' => intval($_GPC['tpltype']),
        'sms_enable' => intval($_GPC['sms_enable']),
        'sms_username' => trim($_GPC['sms_username']),
        'isneedfollow' => intval($_GPC['isneedfollow']),
        'follow_url' => trim($_GPC['follow_url']),
        'share_title' => trim($_GPC['share_title']),
        'share_desc' => trim($_GPC['share_desc']),
        'share_image' => trim($_GPC['share_image']),
        'sms_pwd' => trim($_GPC['sms_pwd']),
        'sms_mobile' => trim($_GPC['sms_mobile']),
        'link_card' => trim($_GPC['link_card']),
        'link_sign' => trim($_GPC['link_sign']),
        'link_card_name' => trim($_GPC['link_card_name']),
        'link_sign_name' => trim($_GPC['link_sign_name']),
        'email_enable' => intval($_GPC['email_enable']),
        'email_host' => $_GPC['email_host'],
        'email_send' => $_GPC['email_send'],
        'email_pwd' => $_GPC['email_pwd'],
        'email_user' => $_GPC['email_user'],
        'email' => trim($_GPC['email']),
        'dateline' => TIMESTAMP,
        'getcash_price' => intval($_GPC['getcash_price']),
        'fee_rate' => floatval($_GPC['fee_rate']),
        'fee_min' => intval($_GPC['fee_min']),
        'fee_max' => intval($_GPC['fee_max']),
        'wechat' => intval($_GPC['wechat']),
        'alipay' => intval($_GPC['alipay']),
        'credit' => intval($_GPC['credit']),
        'is_speaker' => intval($_GPC['is_speaker']),
        'delivery' => intval($_GPC['delivery']),
        'is_commission' => intval($_GPC['is_commission']),
        'commission_level' => intval($_GPC['commission_level']),
        'commission1_rate_max' => floatval($_GPC['commission1_rate_max']),
        'commission1_value_max' => intval($_GPC['commission1_value_max']),
        'commission2_rate_max' => floatval($_GPC['commission2_rate_max']),
        'commission2_value_max' => intval($_GPC['commission2_value_max']),
        'commission3_rate_max' => floatval($_GPC['commission3_rate_max']),
        'commission3_value_max' => intval($_GPC['commission3_value_max']),
        'commission_settlement' => intval($_GPC['commission_settlement']),
    );

    if ($data['is_commission'] == 1) {
        if ($data['commission1_rate_max'] <= 0) {
            message('请输入佣金百分比！');
        }
        if ($data['commission_level'] > 1) {
            if ($data['commission2_rate_max'] <= 0) {
                message('请输入二级佣金百分比！');
            }
        }
    }

    if ($data['email_enable'] == 1) {
        if (empty($_GPC['email_send']) || empty($_GPC['email_user']) || empty($_GPC['email_pwd'])) {
            message('请完整填写邮件配置信息', 'refresh', 'error');
        }
        if ($_GPC['email_host'] == 'smtp.qq.com' || $_GPC['email_host'] == 'smtp.gmail.com') {
            $secure = 'ssl';
            $port = '465';
        } else {
            $secure = 'tls';
            $port = '25';
        }

        $mail_config = array();
        $mail_config['host'] = $_GPC['email_host'];
        $mail_config['secure'] = $secure;
        $mail_config['port'] = $port;
        $mail_config['username'] = $_GPC['email_user'];
        $mail_config['sendmail'] = $_GPC['email_send'];
        $mail_config['password'] = $_GPC['email_pwd'];
        $mail_config['mailaddress'] = $_GPC['email'];
        $mail_config['subject'] = '微点餐提醒';
        $mail_config['body'] = '邮箱测试';
        $result = $this->sendmail($mail_config);
    }

    if (empty($setting)) {
        pdo_insert($this->table_setting, $data);
    } else {
        unset($data['dateline']);
        pdo_update($this->table_setting, $data, array('weid' => $_W['uniacid']));
    }
    message('操作成功', $this->createWebUrl('setting'), 'success');
}

include $this->template('web/setting');